# -*- coding:utf-8 -*-
'''
加载pdf文件
简单文本提取 → pypdf
表格数据提取 → pdfplumber
高性能/图片处理 → PyMuPDF (fitz)
扫描件/OCR → pdf2image + pytesseract
'''

import logging
from pypdf import PdfReader
import pdfplumber
import fitz
from pdfminer.high_level import extract_text
from pdf2image import convert_from_path
import pytesseract

# 禁用 pdfplumber 的特定警告
logging.getLogger("pdfplumber").setLevel(logging.ERROR)


def read_pdf(file_path):
    with open(file_path, "rb") as file:
        reader = PdfReader(file)
        text = ""
        for page in reader.pages:
            text += page.extract_text() + "\n"
        return text

def extract_pdf(file_path):
    with pdfplumber.open(file_path) as pdf:
        text = ""
        for page in pdf.pages:
            text += page.extract_text() + "\n"
            # 提取表格：tables = page.extract_tables()
        return text

def read_pdf_fitz(file_path):
    doc = fitz.open(file_path)
    text = ""
    for page in doc:
        text += page.get_text() + "\n"
    return text

if __name__ == '__main__':
    path = '/data/annual report/pdf/2012_600288_大恒科技.pdf'
    # pdf_text = extract_pdf(path)
    # pdf_text = extract_text(path)
    # print(pdf_text)

    images = convert_from_path("/data/annual report/pdf/2011_002331_皖通科技.pdf")
    for i, img in enumerate(images):
        text = pytesseract.image_to_string(img, lang='chi_sim')  # 中文简体
        print(f"Page {i + 1}:\n{text}\n")

    # with pdfplumber.open(path) as pdf:
    #     for page in pdf.pages:
    #         text = page.extract_text()
    #         print(text)